/*********************************************************************
	Name: bisskit_format.c         
	Description: Convert 12 bit data to 8 bit data containing a
                 divisor and range.                  
			
	Author: Andrew Nicholas
	Date: 30/08/2011
*********************************************************************/
#include "bisskit_format.h"

int range = 1;
int divisor = 128;

enum SYNC_STATUS {
     SYNC = 0, STATUS
} packet_t;

enum SYNC_STATUS packet_t  = STATUS;
 
/*****************************************************************************
** Function name:		bisskit_packet
**
** Descriptions:		Convert data to input
**
** parameters:		    None
** Returned value:		None
**
*****************************************************************************/
void bisskit_packet(unsigned int input_data, char output_data, char sync_status){
        
unsigned long int intermed_result = input_data;


    switch (packet_t)
    {   
        // Divisor and range set        
        case STATUS:

		if (input_data < 255000)
            // Set divisor
            if (input_data > 199){
                divisor = 16;
                sync_status |= 0x00;
            }
            else{
                divisor = 128;
                sync_status |= 0x01;
            }

			intermed_result *= divisor;

            // Set range
            if (intermed_result <= 255){
                range = 1;
                sync_status |= 0x00;
            }
            else if (intermed_result <= 2550){
                range = 10;
                sync_status |= 0x02;
            }
            else{
                range = 100;
                sync_status |= 0x06;
            }   
			intermed_result /= range;

			output_data = intermed_result;
			break;

        // Only the divisor is set.
        case SYNC:
            // Set divisor
            if (input_data > 199){
                range = 16;
                sync_status = 0xfe;
            }
            else{
                range = 128;
                sync_status = 0xff;
            }
            intermed_result = input_data * divisor;
            output_data = intermed_result / range;     
			break; 
   }
}
